<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="员工姓名">
        <el-input v-model="queryParams.empName" placeholder="请输入员工姓名"></el-input>
      </el-form-item>
      <el-form-item label="所属部门">
        <el-select v-model="queryParams.deptId" placeholder="请选择部门">
          <el-option
            v-for="item in deptList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="empList" border>
      <el-table-column prop="empId" label="ID" width="80"></el-table-column>
      <el-table-column prop="empName" label="姓名"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column prop="deptName" label="部门"></el-table-column>
      <el-table-column prop="salary" label="薪资"></el-table-column>
      <el-table-column prop="hireDate" label="入职日期"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 添加/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="姓名" prop="empName">
          <el-input v-model="form.empName"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <el-select v-model="form.deptId">
            <el-option
              v-for="item in deptList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="薪资" prop="salary">
          <el-input-number v-model="form.salary" :precision="2" :step="1000"></el-input-number>
        </el-form-item>
        <el-form-item label="入职日期" prop="hireDate">
          <el-date-picker
            v-model="form.hireDate"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeList, addEmployee, updateEmployee, deleteEmployee } from '@/api/employee'
import { getDepartmentList } from '@/api/department'

export default {
  data() {
    return {
      queryParams: {
        empName: '',
        deptId: null,
        page: 1,
        pageSize: 10
      },
      empList: [],
      deptList: [],
      total: 0,
      dialogVisible: false,
      dialogTitle: '',
      form: {
        empId: null,
        empName: '',
        gender: '',
        deptId: null,
        salary: 0,
        hireDate: ''
      },
      rules: {
        empName: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
        gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
        deptId: [{ required: true, message: '请选择部门', trigger: 'change' }],
        salary: [{ required: true, message: '请输入薪资', trigger: 'blur' }],
        hireDate: [{ required: true, message: '请选择入职日期', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
    this.getDeptList()
  },
  methods: {
    async getList() {
      const res = await getEmployeeList(this.queryParams)
      this.empList = res.data.list
      this.total = res.data.total
    },
    async getDeptList() {
      const res = await getDepartmentList()
      this.deptList = res.data
    },
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams = {
        empName: '',
        deptId: null,
        page: 1,
        pageSize: 10
      }
      this.getList()
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.page = val
      this.getList()
    },
    handleAdd() {
      this.dialogTitle = '添加员工'
      this.form = {
        empId: null,
        empName: '',
        gender: '',
        deptId: null,
        salary: 0,
        hireDate: ''
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑员工'
      this.form = { ...row }
      this.dialogVisible = true
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该员工?', '提示', {
          type: 'warning'
        })
        await deleteEmployee(row.empId)
        this.$message.success('删除成功')
        this.getList()
      } catch (error) {
        console.error(error)
      }
    },
    async submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.form.empId) {
              await updateEmployee(this.form)
              this.$message.success('更新成功')
            } else {
              await addEmployee(this.form)
              this.$message.success('添加成功')
            }
            this.dialogVisible = false
            this.getList()
          } catch (error) {
            console.error(error)
          }
        }
      })
    }
  }
}
</script> 